package com.dzt.androidframework.jsoup.detail;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.view.View;

import com.dzt.androidframework.R;
import com.dzt.androidframework.activity.ActivityTitle;
import com.dzt.androidframework.jsoup.detail.bean.ImageDetailDomain;
import com.dzt.androidframework.jsoup.detail.model.RemoteDetailDataSource;
import com.wang.avi.AVLoadingIndicatorView;

import org.sufficientlysecure.htmltextview.HtmlTextView;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

public class NewsDetailActivity extends ActivityTitle implements DetailContract.View {

	@ViewInject(R.id.htNewsContent)
	private HtmlTextView htmlNewsContent;
	@ViewInject(R.id.loadingView)
	private AVLoadingIndicatorView loadingView;
	@ViewInject(R.id.floatBuuton)
	private FloatingActionButton floatButton;

	private ArrayList<ImageDetailDomain> imageDetailDomainList;
	private ImageDetailPresenter presenter;

	@Override
	protected int getLayoutId() {
		return R.layout.activity_news_detail;
	}

	@Override
	protected void initParams() {
		String imageNewUrl = getIntent().getStringExtra("image_new_url");
		String imageUrl = getIntent().getStringExtra("image_url");
		final String imageNewTitle = getIntent().getStringExtra("image_new_title");
		
		setTitleView(imageNewTitle);
		RemoteDetailDataSource dataSource = RemoteDetailDataSource.getInstance();
		presenter = new ImageDetailPresenter(dataSource, this);
		presenter.getImageDetailList(imageNewUrl);
		floatButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (null == imageDetailDomainList) {
					return;
				}
				Intent intent = new Intent(NewsDetailActivity.this, ImageGalleryActivity.class);
				intent.putParcelableArrayListExtra("imageNewList", imageDetailDomainList);
				intent.putExtra("image_new_title", imageNewTitle);
				startActivity(intent);
			}
		});
	}

	@Override
	protected void initData(Bundle savedInstanceState) {

	}

	@Override
	protected String[] initPermissions() {
		return new String[0];
	}

	@Override
	public void setPresenter(DetailContract.Presenter presenter) {

	}

	@Override
	public void GetImageDetailList(List<ImageDetailDomain> imageDetailDomainList, String article) {
		this.imageDetailDomainList = (ArrayList) imageDetailDomainList;
		htmlNewsContent.setHtmlFromString(article, new HtmlTextView.RemoteImageGetter());
	}

	@Override
	public void showLoading() {
		loadingView.setVisibility(View.VISIBLE);
	}

	@Override
	public void hideLoading() {
		loadingView.setVisibility(View.INVISIBLE);
	}

	@Override
	public void showLoadFailed() {
		loadingView.setVisibility(View.INVISIBLE);
	}

	@Override
	public void onBackPressed() {
		//super.onBackPressed();
		//解决按Back退出报错问题
		finish();
	}

	@Override
	protected void onDestroy() {
		htmlNewsContent.destroyDrawingCache();
		htmlNewsContent = null;
		super.onDestroy();
	}
}
